﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.ComponentModel;

using BuildProject = Microsoft.Build.BuildEngine.Project;
using Microsoft.WowAddonStudio.Project.Import.Ace;
using System.IO;
using Microsoft.WowAddonStudio.Project.Import.Folder;

namespace Microsoft.WowAddonStudio.Project.Import
{
    /// <summary>
    /// Class that implements plugin independent import process.
    /// Asynchron execution is possible.
    /// </summary>
    public class ImportProcess : BackgroundWorker
    {
        #region Private members
        private IImportPlugin plugin;
        private object settings;
        private string targetPath;
        private ProjectBuilder builder;

        private double totalTaskCount;
        private double delta;
        private double percentFinished;
        #endregion

        #region Lifecycle methods
        /// <summary>
        /// Creates an instance of <see cref="ImportProcess"/> taking the
        /// plugin and the settings as parameters./>
        /// </summary>
        /// <param name="plugin">Plugin to use. Cannot be null.</param>
        /// <param name="settings">Settings that the plugin will use.</param>
        /// <param name="targetPath">Target path to which copies will be made and the
        /// project is built.</param>
        public ImportProcess(IImportPlugin plugin, object settings, string targetPath)
        {
            if (plugin == null)
                throw new ArgumentNullException("Plugin should be specified.");

            if (string.IsNullOrEmpty(targetPath))
                throw new ArgumentNullException("targetPath");

            this.plugin = plugin;

            //Null import settings can be allowed...
            this.settings = settings;
            this.targetPath = targetPath;

            builder = new ProjectBuilder();
            builder.ReportProgress += new EventHandler(builder_ReportProgress);

            WorkerReportsProgress = true;
            WorkerSupportsCancellation = true;
        }
        #endregion

        #region Protected overrides
        /// <summary>
        /// Implements the import process.
        /// </summary>
        protected override void OnDoWork(DoWorkEventArgs e)
        {
            base.OnDoWork(e);
            ReportProgress(0);

            //Get the import list
            IEnumerable<IAddonItem> items=this.plugin.Import(settings);

			if (plugin is AceImportPlugin)
				this.targetPath = Path.Combine(targetPath, (plugin as AceImportPlugin).RootDirectoryName);

			if (plugin is FolderImportPlugin)
				this.targetPath = Path.Combine(targetPath, (plugin as FolderImportPlugin).RootDirectoryName);

            totalTaskCount=items.Count<IAddonItem>()+2;
            delta=100.0/totalTaskCount;
            percentFinished = delta;
            ReportProgress((int)percentFinished);
            
            //Build the project
            BuildProject project = builder.Build(targetPath, items);

            TempFileContainer.Instance.DeleteTempFiles();

            ReportProgress(100);
            e.Result = project.FullFileName;
        }
        #endregion

        #region Private member functions
        private void builder_ReportProgress(object sender, EventArgs e)
        {
            percentFinished += delta;
            ReportProgress((int)percentFinished);
        }
        #endregion
    }
}